#include "login.h"
#include "ticketing.h"
#include "myfont.h"
#include "admin.h"

#define DISP_BUF_SIZE (128 * 1024)

/*登录界面全局变量*/
lv_obj_t *my_admin=NULL;//管理员窗口
lv_obj_t *login = NULL;//登录窗口
lv_obj_t *today = NULL;//今日日期窗口
char now_Date[11];//今日日期
char date_str[11];  // 存储日期

lv_obj_t *login_btn = NULL;//登录按钮
lv_obj_t *logon_btn = NULL;//注册按钮
lv_obj_t *go_back_btn=NULL;//返回登录按钮
lv_obj_t *login_keyboard = NULL;//登录键盘
lv_obj_t *login_username_input = NULL;//登录用户名输入框
lv_obj_t *login_password_input = NULL;//登录密码输入框
lv_obj_t *logon_username_input = NULL;//注册用户名输入框
lv_obj_t *logon_password_input = NULL;//注册密码输入框
lv_obj_t *logon_password_input2 = NULL;//注册密码输入框2
lv_obj_t *logon_error_popup = NULL;//错误提示弹窗
lv_obj_t *jpg_bg = NULL; 
lv_obj_t *user_dropdown= NULL;//用户下拉框



lv_style_t go_back_btn_style;//返回登录按钮样式

/*取票界面全局变量*/

lv_obj_t *ticket=NULL;//取票窗口

lv_obj_t *weather_label;//天气label
lv_obj_t *time_label;//时间label
lv_obj_t *ticket_popup= NULL;//取票界面弹窗
lv_obj_t *take_ticket_btn= NULL;//取票按钮
lv_obj_t *buy_ticket_btn= NULL;//购票按钮
lv_obj_t *exit_btn= NULL;//退出按钮
lv_obj_t *exit_input_ticket_btn= NULL;//退出输入票号按钮
lv_obj_t *confirm_ticket_btn;//确认取票按钮
lv_obj_t *my_ad;//我的广告

lv_style_t exit_btn_style;//退出按钮样式
lv_style_t exit_intput_ticket_btn_style;


/*买票界面全局变量*/
lv_obj_t *movies_dropdown= NULL;//电影下拉框
lv_obj_t *exit_buy_ticket_btn;//退出买票按钮
lv_style_t exit_buy_ticket_btn_style;//退出购票按钮样式
lv_obj_t *confirm_buy_ticket_btn;//确认购票按钮
lv_obj_t *selectsession_dropdown;//场次下拉框
lv_obj_t *seats_container;//座位容器

/*当前用户名*/
char as_username[20] = {0};
char user_path[100]; //用户路径
/*电影名数组*/
char file_content[1024];
/*用户名数组*/
char user_list[105];


int main(void)
{
    /*LittlevGL init*/
    lv_init();
    
    /*Linux frame buffer device init*/
    fbdev_init();

    /*A small buffer for LittlevGL to draw the screen's content*/
    static lv_color_t buf[DISP_BUF_SIZE];

    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);

    /*Initialize and register a display driver*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf   = &disp_buf;
    disp_drv.flush_cb   = fbdev_flush;
    disp_drv.hor_res    = 800;
    disp_drv.ver_res    = 480;
    lv_disp_drv_register(&disp_drv);

    evdev_init();
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;

    /*This function will be called periodically (by the library) to get the mouse position and state*/
    indev_drv_1.read_cb = evdev_read;
    lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1);


    /*Set a cursor for the mouse*/
    LV_IMG_DECLARE(mouse_cursor_icon)
    lv_obj_t * cursor_obj = lv_img_create(lv_scr_act()); /*Create an image object for the cursor */
    lv_img_set_src(cursor_obj, &mouse_cursor_icon);           /*Set the image source*/
    lv_indev_set_cursor(mouse_indev, cursor_obj);             /*Connect the image  object to the driver*/

    printf("start\n");
    login_init();
    /*Handle LitlevGL tasks (tickless mode)*/
    while(1) {
        
        lv_timer_handler();
        usleep(5000);
    }

    return 0;
}

/*Set in lv_conf.h as `LV_TICK_CUSTOM_SYS_TIME_EXPR`*/
uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if(start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
     }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}

